package cn.stylefeng.guns.modular.tool.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.tool.entity.ToolExpressScan;
import cn.stylefeng.guns.modular.tool.entity.ToolTaskArrange;
import cn.stylefeng.guns.modular.tool.model.params.ToolBasicPackInstallParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressExcelParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressScanParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressSumParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressSumTaskParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolTaskArrangeParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolTaskMoneyParam;
import cn.stylefeng.guns.modular.tool.model.result.ToolBasicPackInstallResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressScanResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolTaskArrangeResult;
import cn.stylefeng.guns.modular.tool.service.ToolBasicPackInstallService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressExcelService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressScanService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressSumService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressSumTaskService;
import cn.stylefeng.guns.modular.tool.service.ToolTaskArrangeService;
import cn.stylefeng.guns.modular.tool.service.ToolTaskMoneyService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 扫码分配控制器
 */
@Controller
@RequestMapping("/toolExpressScan")
public class ToolExpressScanController extends BaseController {

    private String PREFIX = "/tool/toolExpressScan";

    @Autowired
    private ToolExpressScanService toolExpressScanService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/toolExpressScan.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/toolExpressScan_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit(Model model,Long waveId) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	ToolTaskArrangeParam toolTaskArrangeParam = new ToolTaskArrangeParam();
    	toolTaskArrangeParam.setWaveId(waveId);
    	List<ToolTaskArrangeResult> alist = toolTaskArrangeService.findListBySpec(toolTaskArrangeParam);
    	model.addAttribute("alist", alist);
    	ToolBasicPackInstallParam packInstallParam = new ToolBasicPackInstallParam();
    	packInstallParam.setDeptId(deptId);
    	List<ToolBasicPackInstallResult> pilist = toolBasicPackInstallService.findListBySpec(packInstallParam, user.getOperateId());
    	model.addAttribute("pilist", pilist);
        return PREFIX + "/toolExpressScan_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ToolExpressScanParam toolExpressScanParam) {
    	toolExpressScanParam.setState("待更新");
        this.toolExpressScanService.add(toolExpressScanParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ToolExpressScanParam toolExpressScanParam) {
    	ToolTaskArrange toolTaskArrange = toolTaskArrangeService.getById(toolExpressScanParam.getArrangeId());
    	toolExpressScanParam.setArrangeNumber(toolTaskArrange.getGroupNumber());
    	toolExpressScanParam.setUserName(toolTaskArrange.getGroupUsers());
        this.toolExpressScanService.update(toolExpressScanParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ToolExpressScanParam toolExpressScanParam) {
        this.toolExpressScanService.delete(toolExpressScanParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ToolExpressScanParam toolExpressScanParam) {
        ToolExpressScan detail = this.toolExpressScanService.getById(toolExpressScanParam.getId());
        return ResponseData.success(detail);
    }
    
    /**
     * 批量删除接口
     */
    @RequestMapping("/btnRevoke")
    @ResponseBody
    public ResponseData btnRevoke(Integer[] ids) {
    	for (Integer id : ids) {
    		ToolExpressScanParam toolExpressParam = new ToolExpressScanParam();
            toolExpressParam.setId(id);
			this.toolExpressScanService.delete(toolExpressParam );
		}
        return ResponseData.success();
    }

    @Autowired
    private ToolTaskMoneyService toolTaskMoneyService;
    
    /**
     * 清空接口
     */
    @RequestMapping("/btnRevoke72")
    @ResponseBody
    public ResponseData btnRevoke72(ToolExpressScanParam toolExpressScanParam) {
        toolExpressScanService.deleteBySpec(toolExpressScanParam);

        ToolExpressSumTaskParam expressSumTaskParam = new ToolExpressSumTaskParam();
        expressSumTaskParam.setWaveId(toolExpressScanParam.getWaveId());
        toolExpressSumTaskService.deleteBySpec(expressSumTaskParam);

    	ToolTaskMoneyParam moneyParam = new ToolTaskMoneyParam();
    	moneyParam.setWaveId(toolExpressScanParam.getWaveId());
    	toolTaskMoneyService.deleteBySpec(moneyParam);//删除工资明细
        return ResponseData.success();
    }

    @Autowired
    private ToolTaskArrangeService toolTaskArrangeService;
    
    /**
     *分配接口
     */
    @RequestMapping("/choiceuser")
    @ResponseBody
    public ResponseData choiceuser(Integer[] ids,Long arrangeId) {
    	ToolTaskArrange toolTaskArrange = toolTaskArrangeService.getById(arrangeId);
    	for (Integer id : ids) {
    		ToolExpressScanParam toolExpressParam = new ToolExpressScanParam();
            toolExpressParam.setId(id);
            toolExpressParam.setArrangeId(arrangeId);
            toolExpressParam.setArrangeNumber(toolTaskArrange.getGroupNumber());
            toolExpressParam.setUserName(toolTaskArrange.getGroupUsers());
			this.toolExpressScanService.update(toolExpressParam);
		}
        return ResponseData.success();
    }

    @Autowired
    private ToolExpressService toolExpressService;

    @Autowired
    private ToolExpressSumTaskService toolExpressSumTaskService;

    @Autowired
    private ToolBasicPackInstallService toolBasicPackInstallService;
    
    private Lock lock = new ReentrantLock();
    
    /**
     *更新接口
     */
    @RequestMapping("/btnUpdate")
    @ResponseBody
    public ResponseData btnUpdate(ToolExpressScanParam toolExpressScanParam) {
    	User user = userService.getcurrentUser();
		lock.lock();//获取锁
		try {
	    	toolExpressScanParam.setState("待更新");
	    	List<ToolExpressScanResult> list = toolExpressScanService.findListBySpec(toolExpressScanParam);
	    	ToolBasicPackInstallParam toolBasicPackInstallParam = new ToolBasicPackInstallParam();
	    	toolBasicPackInstallParam.setDeptId(user.getDeptId());
	    	List<ToolBasicPackInstallResult> pilist = toolBasicPackInstallService.findListBySpec(toolBasicPackInstallParam,user.getOperateId());
	    	List<ToolExpressSumTaskParam> savelist = new ArrayList<ToolExpressSumTaskParam>();
	    	for (ToolExpressScanResult r : list) {
	    		if(ToolUtil.isEmpty(r.getUserName())) {
	    			continue;
	    		}
	    		ToolExpressParam expressParam = new ToolExpressParam();
	    		expressParam.setWaveId(toolExpressScanParam.getWaveId());
	    		expressParam.setOrderNumber(r.getOrderNumber());
	    		ToolExpressResult expressResult = toolExpressService.findBySpec(expressParam);
	    		if(expressResult != null) {
	    			BigDecimal weight = new BigDecimal(0);
    				if(ToolUtil.isNotEmpty(expressResult.getV2())) {
    					weight = new BigDecimal(expressResult.getV2());
    				}
    				if(ToolUtil.isNotEmpty(r.getV3())) {
    					for (ToolBasicPackInstallResult pi : pilist) {
    						if(r.getV3().equals(pi.getPackNumber())) {
    							if(ToolUtil.isNotEmpty(pi.getWeight())) {
    								BigDecimal piWeight = new BigDecimal(pi.getWeight());
    								weight = weight.add(piWeight).setScale(3, BigDecimal.ROUND_HALF_DOWN);
    							}
    							break;
    						}
    					}
    				} else {
						BigDecimal piWeight = new BigDecimal(0.1);
						weight = weight.add(piWeight).setScale(3, BigDecimal.ROUND_HALF_DOWN);
    				}
	    			ToolExpressSumTaskParam taskParam = new ToolExpressSumTaskParam();
	    			taskParam.setWaveId(toolExpressScanParam.getWaveId());
	    			taskParam.setCustomerId(expressResult.getCustomerId());
	    			taskParam.setCustomerName(expressResult.getCustomerName());
	    			taskParam.setArrangeId(r.getArrangeId());
	    			taskParam.setArrangeNumber(r.getArrangeNumber());
	    			taskParam.setUserName(r.getUserName());
	    			taskParam.setHandover(r.getHandover());
	    			taskParam.setOutNumber(expressResult.getV1());//出库单号
	    			taskParam.setOrderNumber(expressResult.getOrderNumber());//快递单号
	    			taskParam.setGoodsNumber(expressResult.getGoodsNumber());//商品组合
	    			taskParam.setNum(expressResult.getNum()+"");//组合数量
	    			taskParam.setSumNum("1");//单量
	    			taskParam.setV1(expressResult.getNum()+"");//作业量
	    			taskParam.setV2(r.getV1());//波次
	    			taskParam.setV3(r.getV2());//类型
	    			taskParam.setV4(weight.toString());//重量
	    			taskParam.setV5(expressResult.getV3());//快递公司
	    			taskParam.setDeptId(user.getDeptId());
	    			savelist.add(taskParam);
					
					ToolExpressScanParam expressScanParam = new ToolExpressScanParam();
					expressScanParam.setId(r.getId());
					expressScanParam.setState("已更新");
					toolExpressScanService.update(expressScanParam);
	    		}
			}
	    	if(savelist.size()>0) {
	    		toolExpressSumTaskService.saveBatchList(savelist);
	    	}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();//释放锁
		}
        return ResponseData.success();
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ToolExpressScanParam toolExpressScanParam,String condition ) {
        return this.toolExpressScanService.findPageBySpec(toolExpressScanParam,condition);
    }

    
}


